//
//  FQDLabFeedCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabFeedCell.h"
#import "FQDFeedCellTitleBarView.h"
#import "FQDLabFeedTitleView.h"
#import "FQDRestModel.h"
#import "FQDLabFeedCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"


@interface FQDLabFeedCell ()

// 标题栏
@property (nonatomic, strong) FQDFeedCellTitleBarView *titleBarView;
// 内容
@property (nonatomic, strong) FQDLabFeedTitleView *titleView;

@end

@implementation FQDLabFeedCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _showContentImage = YES;
        _showSubscription = NO;
        [self prepareUI];
        [self updateTitleBarView];
    }
    return self;
}

- (void)prepareUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    _titleBarView = [[FQDFeedCellTitleBarView alloc] init];
    _titleBarView.followButton.hidden = YES;
    _titleBarView.userInteractionEnabled = YES;
    [self.contentView addSubview:_titleBarView];
    
    [_titleBarView.titleBarButton addTarget:self action:@selector(titleBarTapped:)
                           forControlEvents:UIControlEventTouchUpInside];
    
    _titleView = [[FQDLabFeedTitleView alloc] init];
    [self.contentView addSubview:_titleView];
    
    WeakSelf;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(weakSelf);
    }];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleBarView.mas_bottom);
        make.left.bottom.and.right.equalTo(weakSelf.contentView);
    }];
}

- (void)updateTitleBarView
{
    CGFloat titleBarHeight;
    if (self.showSubscription) {
        self.titleBarView.hidden = NO;
        titleBarHeight = 50.0f;
    } else {
        self.titleBarView.hidden = YES;
        titleBarHeight = 0;
    }
    
    WeakSelf;
    [self.titleBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView);
        make.width.equalTo(weakSelf.contentView);
        make.height.equalTo(@(titleBarHeight));
    }];
}

- (void)configureCellWithCellItem:(FQDLabFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDFeed *feed = cellItem.feed;
    
    FQDColumn *column = feed.post.column;
    [self.titleBarView.iconView sd_setImageWithURL:[NSURL URLWithString:column.icon]];
    [self.titleBarView.titleLabel setText:column.name];
    
    self.titleView.post = feed.post;
    self.showSubscription = cellItem.showSubscription;
    [self updateTitleBarView];
}

- (void)titleBarTapped:(UIButton *)titleBarButton
{
    if ([self.delegate respondsToSelector:@selector(collectionViewCell:actionType:actionControl:userInfo:)]) {
        [self.delegate collectionViewCell:self actionType:FQDLabFeedCellActionTitleBarClicked
                            actionControl:titleBarButton userInfo:nil];
    }
}

#pragma mark - Getters and Setters
- (void)setShowContentImage:(BOOL)showContentImage
{
    _showContentImage = showContentImage;
    self.titleView.hideTitleImage = !showContentImage;
}

+ (NSString *)reuseId
{
    return @"LabFeedCellId";
}

@end
